package com.sjk.flink.relevance;

import com.sjk.flink.exeception.BaseException;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.HashMap;
import java.util.Map;

public class RelevanceFactory {


    private Map<String,RelevanceValidator> validators = new HashMap<String,RelevanceValidator>();

    //私有构造方法
    private RelevanceFactory(){}

    //静态变量创建类的对象
    private static RelevanceFactory instance = new RelevanceFactory();

    private Jedis jedis;

    //对外提供静态方法获取该对象
    public static RelevanceFactory getInstance(){
        return instance;
    }

    public synchronized void putJedisCluster(Jedis jedis){
        this.jedis = jedis;
    }

    public synchronized boolean relevance(String key){
        RelevanceValidator validator = validators.get(key);
        if(validator!=null){
            return  true;
        }
        return  false;
    }

    public synchronized void addValidator(String key,RelevanceValidator validator){
        validators.put(key,validator);
    }


    public boolean check(String key,String json) {
        RelevanceValidator validator = validators.get(key);
        if (validator == null) {
            throw new BaseException("检查对象不存在");
        }
        return validator.doCheck(jedis,json);
    }

}
